<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.4"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>miniFOC: algorithm/filter.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="Acme_LOGO.png"/></td>
  <td id="projectalign">
   <div id="projectname">miniFOC<span id="projectnumber">&#160;1.0.3</span>
   </div>
   <div id="projectbrief">This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_99d3723365464dd656f512988bb8dbbd.html">algorithm</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">filter.c</div></div>
</div><!--header-->
<div class="contents">
<a href="filter_8c.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="comment">/**************************************************************************/</span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span><span class="preprocessor">#include &quot;<a class="code" href="filter_8h.html">filter.h</a>&quot;</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="preprocessor">#include &quot;<a class="code" href="system_8h.html">system.h</a>&quot;</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span><span class="preprocessor">#include &quot;<a class="code" href="foc_8h.html">foc.h</a>&quot;</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span><span class="preprocessor">#include &quot;<a class="code" href="config_8h.html">config.h</a>&quot;</span></div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span> </div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"><a class="line" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">   19</a></span><span class="keyword">volatile</span> <a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> <a class="code hl_variable" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a>;</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span> </div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno"><a class="line" href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">   28</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">filter_coefficient_config</a>(<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *param, <span class="keywordtype">float</span> cutoff_freq, <span class="keywordtype">float</span> sample_freq, <span class="keywordtype">float</span> coefficient) {</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>    <span class="comment">/* clear the value of the first order low pass filter parameter handler */</span></div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>    <a class="code hl_function" href="system_8c.html#a7e7cece1b30ef67968b244698f999b0e">user_memset</a>(param, 0x00, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a>));</div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span> </div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span>    <span class="comment">/* the RC time constant is calculated from the cut-off frequency */</span></div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>    <span class="keywordtype">float</span> time_const = 1.0f / (6.2831852f * cutoff_freq);</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span> </div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span>    <span class="comment">/* calculation of digital low-pass filter coefficients */</span></div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span>    param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a95c29040dec4c9e97cf2f64cf4212435">coefficient1</a> = (1.0f / (1.0f + time_const * sample_freq)) * coefficient;</div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span>    param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a08c1824c5523caa58ec64b9fcfd48f92">coefficient2</a> = (time_const * sample_freq) / (1.0f + time_const * sample_freq);</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span>    param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a> = 0;</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>    param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">last_result</a> = 0;</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>}</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span> </div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"><a class="line" href="filter_8c.html#aa1e7128fffcf2b56a6fb1c23f89afd75">   45</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="filter_8c.html#aa1e7128fffcf2b56a6fb1c23f89afd75">filter_config</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>    <a class="code hl_function" href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">filter_coefficient_config</a>((<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *) &amp;<a class="code hl_variable" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a>,</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>                              <a class="code hl_define" href="config_8h.html#ae9289eb166344bb9d0d6560b6da41118">TIM13_FREQUENCY</a>, <a class="code hl_define" href="config_8h.html#adddd943909a2fac22c5fe8e3949d105f">SPEED_UP_FREQ</a>, <a class="code hl_define" href="foc_8h.html#a2ce50cab9f20bc664e4888ffc07e656e">SPEED_COEFFICIENT</a>);</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>}</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span> </div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"><a class="line" href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">   56</a></span><span class="keywordtype">float</span> <a class="code hl_function" href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">filter_update_value</a>(<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *param, <span class="keywordtype">short</span> value) {</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>    <span class="comment">/* judge whether the input parameters are abnormal,</span></div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span><span class="comment">     * calculate the output value of the first-order low-pass filter */</span></div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span>    <span class="keywordflow">if</span> (value &lt; 200 &amp;&amp; value &gt; -200)</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>        param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a> = param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a95c29040dec4c9e97cf2f64cf4212435">coefficient1</a> * (float) value + param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a08c1824c5523caa58ec64b9fcfd48f92">coefficient2</a> * param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">last_result</a>;</div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span> </div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span>    <span class="comment">/* update the output value of the previous time */</span></div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>    param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">last_result</a> = param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a>;</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>    <span class="keywordflow">return</span> param-&gt;<a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a>;</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>}</div>
<div class="ttc" id="aconfig_8h_html"><div class="ttname"><a href="config_8h.html">config.h</a></div><div class="ttdoc">used to place important parameter configurations for users</div></div>
<div class="ttc" id="aconfig_8h_html_adddd943909a2fac22c5fe8e3949d105f"><div class="ttname"><a href="config_8h.html#adddd943909a2fac22c5fe8e3949d105f">SPEED_UP_FREQ</a></div><div class="ttdeci">#define SPEED_UP_FREQ</div><div class="ttdoc">set speed update frequency to 200Hz</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00032">config.h:32</a></div></div>
<div class="ttc" id="aconfig_8h_html_ae9289eb166344bb9d0d6560b6da41118"><div class="ttname"><a href="config_8h.html#ae9289eb166344bb9d0d6560b6da41118">TIM13_FREQUENCY</a></div><div class="ttdeci">#define TIM13_FREQUENCY</div><div class="ttdoc">set speed/angle PID loop frequency to 100Hz</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00030">config.h:30</a></div></div>
<div class="ttc" id="afilter_8c_html_a07532054f0695f8abd4f68cece7b2f7e"><div class="ttname"><a href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">filter_coefficient_config</a></div><div class="ttdeci">void filter_coefficient_config(Filter_Structure_t *param, float cutoff_freq, float sample_freq, float coefficient)</div><div class="ttdoc">configure low-pass filter parameters</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00028">filter.c:28</a></div></div>
<div class="ttc" id="afilter_8c_html_a687b2ec6984ce9fa9d08189a3e7e1592"><div class="ttname"><a href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">filter_update_value</a></div><div class="ttdeci">float filter_update_value(Filter_Structure_t *param, short value)</div><div class="ttdoc">update the output value of the first-order low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00056">filter.c:56</a></div></div>
<div class="ttc" id="afilter_8c_html_a70331d536c7d5e99a76bd8651fd6b714"><div class="ttname"><a href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a></div><div class="ttdeci">volatile Filter_Structure_t velocity_filter</div><div class="ttdoc">motor rotation speed low pass filter handle</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00019">filter.c:19</a></div></div>
<div class="ttc" id="afilter_8c_html_aa1e7128fffcf2b56a6fb1c23f89afd75"><div class="ttname"><a href="filter_8c.html#aa1e7128fffcf2b56a6fb1c23f89afd75">filter_config</a></div><div class="ttdeci">void filter_config(void)</div><div class="ttdoc">configure low-pass filter parameters</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00045">filter.c:45</a></div></div>
<div class="ttc" id="afilter_8h_html"><div class="ttname"><a href="filter_8h.html">filter.h</a></div><div class="ttdoc">this is the header file of filter.c, which defines the structure of filter algorithm.</div></div>
<div class="ttc" id="afoc_8h_html"><div class="ttname"><a href="foc_8h.html">foc.h</a></div><div class="ttdoc">this is the header file of foc.c, which defines the structure of FOC algorithm and angle conversion f...</div></div>
<div class="ttc" id="afoc_8h_html_a2ce50cab9f20bc664e4888ffc07e656e"><div class="ttname"><a href="foc_8h.html#a2ce50cab9f20bc664e4888ffc07e656e">SPEED_COEFFICIENT</a></div><div class="ttdeci">#define SPEED_COEFFICIENT</div><div class="ttdoc">mechanical rotate speed conversion factor</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00028">foc.h:28</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html"><div class="ttname"><a href="struct_filter___structure__t.html">Filter_Structure_t</a></div><div class="ttdoc">structure of low-pass filter algorithm</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00016">filter.h:16</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a08c1824c5523caa58ec64b9fcfd48f92"><div class="ttname"><a href="struct_filter___structure__t.html#a08c1824c5523caa58ec64b9fcfd48f92">Filter_Structure_t::coefficient2</a></div><div class="ttdeci">float coefficient2</div><div class="ttdoc">low-pass filter coefficient 2</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00018">filter.h:18</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a72864a941ae97fcfd272e9493d6cb6c7"><div class="ttname"><a href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">Filter_Structure_t::last_result</a></div><div class="ttdeci">float last_result</div><div class="ttdoc">previous calculation results of low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00019">filter.h:19</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a845b4ebfab6dc03b5babd7f9ff5e2b6c"><div class="ttname"><a href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">Filter_Structure_t::current_result</a></div><div class="ttdeci">float current_result</div><div class="ttdoc">current calculation results of low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00020">filter.h:20</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a95c29040dec4c9e97cf2f64cf4212435"><div class="ttname"><a href="struct_filter___structure__t.html#a95c29040dec4c9e97cf2f64cf4212435">Filter_Structure_t::coefficient1</a></div><div class="ttdeci">float coefficient1</div><div class="ttdoc">low-pass filter coefficient 1</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00017">filter.h:17</a></div></div>
<div class="ttc" id="asystem_8c_html_a7e7cece1b30ef67968b244698f999b0e"><div class="ttname"><a href="system_8c.html#a7e7cece1b30ef67968b244698f999b0e">user_memset</a></div><div class="ttdeci">void user_memset(void *buf, unsigned char data, unsigned char num)</div><div class="ttdoc">format buffer array to fixed value, replace memset function</div><div class="ttdef"><b>Definition:</b> <a href="system_8c_source.html#l00049">system.c:49</a></div></div>
<div class="ttc" id="asystem_8h_html"><div class="ttname"><a href="system_8h.html">system.h</a></div><div class="ttdoc">system basic function header file</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.4
</small></address>
</body>
</html>
